package com.baozun.houji.goods.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.bindadapter.CustomBindAdapter
import com.baozhun.mall.common.model.bean.ImageViewInfo
import com.baozhun.mall.common.model.enums.GoodsSource
import com.baozhun.mall.common.model.enums.GoodsType
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.util.eventbus.EventBusUtils
import com.baozhun.mall.common.util.eventbus.EventCode
import com.baozhun.mall.common.util.eventbus.EventMessage
import com.baozhun.mall.common.widget.NumIndicator
import com.baozhun.mall.common.widget.dialog.SkuSelectDialog
import com.baozun.houji.goods.R
import com.baozun.houji.goods.databinding.ActivityGoodsDetailBinding
import com.baozun.houji.goods.viewmodel.GoodsDetailViewModel
import com.hjq.bar.OnTitleBarListener
import com.jaeger.library.StatusBarUtil
import com.previewlibrary.GPreviewBuilder
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.listener.OnBannerListener
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * @Title：商品详情页面
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/5/31
 * @Version：1.0.0
 */
@Route(path = RoutePath.Home.GOODS_DETAIL)
class GoodsDetailActivity : BaseActivity<GoodsDetailViewModel, ActivityGoodsDetailBinding>(), OnTitleBarListener, OnBannerListener<String> {
    private val mGoodsId: String by lazy { intent.getStringExtra(Constants.Goods.GOODS_ID).toString() }
    private val mGoodsSource: String by lazy { intent.getStringExtra(Constants.Goods.GOODS_SOURCE).toString() }
    private val mGoodsType: GoodsType? by lazy { if (mGoodsSource == GoodsSource.SELF.type) GoodsType.SELF else GoodsType.CHANNEL }
    private var mSkuSelectDialog: SkuSelectDialog? = null

    companion object {

        fun start(context: Context, goodsId: String?, goodsSource: String) {
            context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
                putExtra(Constants.Goods.GOODS_ID, goodsId)
                putExtra(Constants.Goods.GOODS_SOURCE, goodsSource)
            })
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        EventBusUtils.register(this)
        StatusBarUtil.setColor(this, getColor(com.baozhun.mall.res.R.color.trans_parent))
        StatusBarUtil.setTransparentForImageView(this, null)
        StatusBarUtil.setLightMode(this)

    }


    override fun initData() {
        super.initData()
        mViewModel.getGoodsInfo(mGoodsId, mGoodsType?.type)
        mViewModel.getRecommendGoods(mGoodsId, mGoodsType?.type)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage<Any>) {
        if (event.eventCode == EventCode.LOGIN_SUCCESS) {
            mViewModel.getGoodsInfo(mGoodsId, mGoodsType?.type)
        } else if (event.eventCode == EventCode.CREATE_ORDER_SUCCESS) {
            mViewModel.getGoodsInfo(mGoodsId, mGoodsType?.type)
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mGoodsDetailBean.observe(this) { it ->
            mSkuSelectDialog = null
            mViewModel.mChooseSkuInfo.value = getString(com.baozhun.mall.res.R.string.please_choose_spec)
            mDataBinding.bannerView.setAdapter(object : BannerImageAdapter<String?>(it.item_imgs as List<String?>?) {
                override fun onBindView(holder: BannerImageHolder, data: String?, position: Int, size: Int) {
                    CustomBindAdapter.loadImageUrl(holder.imageView, data)
                }
            }).addBannerLifecycleObserver(this)//添加生命周期观察者
                .setOnBannerListener(this)
                .setIndicator(NumIndicator(this)).start()
        }
        mViewModel.mRecommendGoodsInfoBean.observe(this) { it ->
            mDataBinding.viewHotRecommend.setData(it)
        }
    }


    override fun onClick(v: View?) {
        super.onClick(v)
        //没获取到数据的情况下除了返回按钮其他按钮都无效
        if (v?.id != R.id.iv_back && mViewModel.mGoodsDetailBean.value == null) return
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mDataBinding.bannerView.destroy()
    }

    override fun OnBannerClick(data: String?, position: Int) {
        if (mViewModel.mGoodsDetailBean.value?.item_imgs.isNullOrEmpty()) return
        GPreviewBuilder.from(this) //activity实例必须
            .setData(mViewModel.mGoodsDetailBean.value?.item_imgs!!.map {
                ImageViewInfo(it).apply {
                    mDataBinding.bannerView.getGlobalVisibleRect(bounds)
                }
            }).setCurrentIndex(position)
            .setType(GPreviewBuilder.IndicatorType.Number) //指示器类型
            .start() //启动
    }

}